package com.nd.controller.wx;

import com.nd.pojo.bean.Coupon;
import com.nd.pojo.bo.wx.CouponExchangeBo;
import com.nd.pojo.bo.wx.CouponReceiveBo;
import com.nd.pojo.vo.wx.BaseRespWXVo;
import com.nd.pojo.vo.wx.CouponDataVo;
import com.nd.service.wx.CouponWxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("wx/coupon")
public class CouponWxController {

    @Autowired
    CouponWxService couponWxService;

    @RequestMapping("mylist")
    public BaseRespWXVo query(Short status, Integer page, Integer size){

        CouponDataVo couponWxQueryDataVo = couponWxService.query(status, page, size);

        return BaseRespWXVo.ok(couponWxQueryDataVo);

    }

    @RequestMapping("list")
    public BaseRespWXVo list(Integer page, Integer size){

        CouponDataVo couponDataVo = couponWxService.list(page, size);

        return BaseRespWXVo.ok(couponDataVo);
    }

    @RequestMapping("receive")
    public BaseRespWXVo receive(@RequestBody CouponReceiveBo couponReceiveBo) {


        Integer code = couponWxService.receive(couponReceiveBo.getCouponId());

        if (code == 1){
            return BaseRespWXVo.ok();
        }else if(code == -2){
            return BaseRespWXVo.fail("已领取");
        }else if(code == 8){
            return BaseRespWXVo.fail("已过期");
        }else if(code == 7){
            return BaseRespWXVo.fail("已下架");
        }
        else {
            return BaseRespWXVo.fail();
        }

    }

    @RequestMapping("selectlist")
    public BaseRespWXVo selectList(Integer cartId, Integer groupRulesId){

        List<Coupon> coupons = couponWxService.selectlist(cartId, groupRulesId);

        return BaseRespWXVo.ok(coupons);

    }

    @RequestMapping("exchange")
    public BaseRespWXVo exchange(@RequestBody CouponExchangeBo couponExchangeBo){

        Integer code = couponWxService.exchange(couponExchangeBo.getCode());

        if (code == 1) {
            return BaseRespWXVo.ok("兑换成功");
        }else if((code == 0) || (code == -1)){
            return BaseRespWXVo.fail("兑换失败");
        }else{
            return BaseRespWXVo.fail("已过期或下架");
        }



    }


}
